package chp10.home;

import java.util.Scanner;

public class StartGuess {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int userScore = 0;
        int opponentScore = 0;
        String choiceName = "";

        String name;
        while (true) {
            // 角色选择
            while (true) {
                System.out.print("请选择对方角色(1: 刘备 2: 孙权 3: 曹操): ");
                int choice;
                try {
                    choice = input.nextInt();
                } catch (Exception e) {
                    System.out.println("请输入有效的数字！");
                    input.nextLine(); // 清除缓冲区
                    continue;
                }

                if (choice == 1) {
                    choiceName = "刘备";
                    break;
                } else if (choice == 2) {
                    choiceName = "孙权";
                    break;
                } else if (choice == 3) {
                    choiceName = "曹操";
                    break;
                } else {
                    System.out.println("请输入1, 2, 3");
                }
            }

            System.out.print("请输入你的姓名: ");
            name = input.next();
            System.out.println(name + " VS " + choiceName);

            System.out.print("要开始游戏吗? (Y/N): ");
            String answer = input.next();

            if (answer.equalsIgnoreCase("Y")) {
                while (true) {
                    System.out.print("请输入你的猜测: 1. 剪刀 2. 石头 3. 布 (请输入相应的数字): ");
                    int guess;
                    try {
                        guess = input.nextInt();
                    } catch (Exception e) {
                        System.out.println("请输入有效的数字！");
                        input.nextLine(); // 清除缓冲区
                        continue;
                    }

                    if (guess < 1 || guess > 3) {
                        System.out.println("请输入1到3之间的数字！");
                        continue;
                    }

                    int random = (int) (Math.random() * 3) + 1;
                    String[] options = {"剪刀", "石头", "布"};
                    System.out.println(choiceName + "选择了: " + options[random - 1]);

                    if (guess == random) {
                        System.out.println("结果: 平局!");
                    } else if ((guess == 1 && random == 2) || (guess == 2 && random == 3) || (guess == 3 && random == 1)) {
                        System.out.println("结果: 再接再厉, 你输了!");
                        opponentScore++;
                    } else {
                        System.out.println("结果: 恭喜, 你赢了!");
                        userScore++;
                    }

                    System.out.println(name + " 得分: " + userScore + " | " + choiceName + " 得分: " + opponentScore);

                    System.out.print("是否继续本轮游戏? (Y/N): ");
                    answer = input.next();
                    if (!answer.equalsIgnoreCase("Y")) {
                        break;
                    }
                }
            } else {
                System.out.print("是否开始下一轮游戏? (Y/N): ");
                answer = input.next();
                if (!answer.equalsIgnoreCase("Y")) {
                    break;
                }
            }
        }

        System.out.println("最终得分:");
        System.out.println(name + " 得分: " + userScore);
        System.out.println(choiceName + " 得分: " + opponentScore);
        input.close();
    }
}